package org.example.work2.tcc.account.mapper;

import org.apache.ibatis.annotations.*;
import org.example.work2.tcc.account.dto.Account;

import java.util.List;

@Mapper
public interface AccountMapper {
	
	@Insert("insert into account_service.tcc_account (account_code, account_name, account_scores, create_time)" + "values (#{accountCode}, #{accountName}, #{accountScores}, now())")
	int insertAccount(Account account);
	
	@Select("select * from account_service.tcc_account")
	@Results({ @Result(column = "accountCode", property = "accountCode"), @Result(column = "create_time", property = "createTime") })
	List<Account> findAllAccount();
	
	@Select("select * from account_service.tcc_account where account_code=#{accountCode}")
	@Results({ @Result(column = "account_code", property = "accountCode"),
			@Result(column = "account_scores", property = "accountScores"),
			@Result(column = "account_name", property = "accountName"),
			@Result(column = "create_time", property = "createTime") })
	Account findAccountByCode(String accountCode);

	@Update("UPDATE account_service.tcc_account SET frozen_scores = #{frozenScores} WHERE account_code = #{accountCode}")
	int updateScores(Account account);

	@Update("UPDATE account_service.tcc_account SET account_scores = account_scores-#{frozenScores} WHERE account_code = #{accountCode}")
	int updateScoresCommit(Account account);

	@Update("UPDATE account_service.tcc_account SET frozen_scores = 0 WHERE account_code = #{accountCode}")
	int updateScoresRollBack(Account account);
}
